package lock版;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author zfh
 * @create 2021-07-28 12:28
 */


class Customer implements Runnable{
    private ReentrantLock lock=new ReentrantLock();
    private static int total=0;
    public void run()
    {
        while(true){
        lock.lock();
        if(total<3000)
        {

            try{
                try {
                    Thread.sleep(1000);
                }
                catch(InterruptedException e)
                {
                    e.printStackTrace();
                }
          System.out.println(Customer.cun(1000))  ;
            }finally{
                lock.unlock();
            }
        }else{
            break;
        }
    }
    }
    public static  String cun(int money)
    {
        total+=money;
        return Thread.currentThread().getName()+":"+"total:"+total;
    }

}
public class BankTest {
    public static void main(String[]args)
    {
       Customer B=new Customer();
        Thread t1=new Thread(B);
        Thread t2=new Thread(B);
        t1.setName("账户1");
        t2.setName("账户2");

        t1.start();
        t2.start();


    }
}
